home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / DolphinVS / DolphinVS.cpp < prev    next >
C/C++ Source or Header  |  2001-10-31  |  28KB  |  709 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Dolphin.cpp
  3. //
  4. // Desc: Sample of swimming dolphin
  5. //
  6. //       Note: This code uses the D3D Framework helper library.
  7. //
  8. // Copyright (c) 1998-2001 Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <stdio.h>
  12. #include <D3DX8.h>
  13. #include "D3DApp.h"
  14. #include "D3DFile.h"
  15. #include "D3DFont.h"
  16. #include "D3DUtil.h"
  17. #include "DXUtil.h"
  18.  
  19.  
  20.  
  21.  
  22. //-----------------------------------------------------------------------------
  23. // Globals variables and definitions
  24. //-----------------------------------------------------------------------------
  25. #define WATER_COLOR         0x00004080
  26.  
  27. struct D3DVERTEX
  28. {
  29.     D3DXVECTOR3 p;
  30.     D3DXVECTOR3 n;
  31.     FLOAT       tu, tv;
  32. };
  33.  
  34. #define D3DFVF_D3DVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
  35.  
  36.  
  37.  
  38.  
  39. //-----------------------------------------------------------------------------
  40. // Name: class CMyD3DApplication
  41. // Desc: Main class to run this application. Most functionality is inherited
  42. //       from the CD3DApplication base class.
  43. //-----------------------------------------------------------------------------
  44. class CMyD3DApplication : public CD3DApplication
  45. {
  46.     // Font for drawing text
  47.     CD3DFont* m_pFont;
  48.  
  49.     // Transform matrices
  50.     D3DXMATRIX              m_matWorld;
  51.     D3DXMATRIX              m_matView;
  52.     D3DXMATRIX              m_matProj;
  53.  
  54.     // Dolphin object
  55.     LPDIRECT3DTEXTURE8      m_pDolphinTexture;
  56.     LPDIRECT3DVERTEXBUFFER8 m_pDolphinVB1;
  57.     LPDIRECT3DVERTEXBUFFER8 m_pDolphinVB2;
  58.     LPDIRECT3DVERTEXBUFFER8 m_pDolphinVB3;
  59.     LPDIRECT3DINDEXBUFFER8  m_pDolphinIB;
  60.     DWORD                   m_dwNumDolphinVertices;
  61.     DWORD                   m_dwNumDolphinFaces;
  62.     DWORD                   m_dwDolphinVertexShader;
  63.     DWORD                   m_dwDolphinVertexShader2;
  64.  
  65.     // Seafloor object
  66.     LPDIRECT3DTEXTURE8      m_pSeaFloorTexture;
  67.     LPDIRECT3DVERTEXBUFFER8 m_pSeaFloorVB;
  68.     LPDIRECT3DINDEXBUFFER8  m_pSeaFloorIB;
  69.     DWORD                   m_dwNumSeaFloorVertices;
  70.     DWORD                   m_dwNumSeaFloorFaces;
  71.     DWORD                   m_dwSeaFloorVertexShader;
  72.     DWORD                   m_dwSeaFloorVertexShader2;
  73.  
  74.     // Water caustics
  75.     LPDIRECT3DTEXTURE8      m_pCausticTextures[32];
  76.     LPDIRECT3DTEXTURE8      m_pCurrentCausticTexture;
  77.  
  78. public:
  79.     HRESULT OneTimeSceneInit();
  80.     HRESULT InitDeviceObjects();
  81.     HRESULT RestoreDeviceObjects();
  82.     HRESULT InvalidateDeviceObjects();
  83.     HRESULT DeleteDeviceObjects();
  84.     HRESULT Render();
  85.     HRESULT FrameMove();
  86.     HRESULT FinalCleanup();
  87.     HRESULT ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format );
  88.  
  89.     CMyD3DApplication();
  90. };
  91.  
  92.  
  93.  
  94.  
  95. //-----------------------------------------------------------------------------
  96. // Name: WinMain()
  97. // Desc: Entry point to the program. Initializes everything, and goes into a
  98. //       message-processing loop. Idle time is used to render the scene.
  99. //-----------------------------------------------------------------------------
  100. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  101. {
  102.     CMyD3DApplication d3dApp;
  103.  
  104.     if( FAILED( d3dApp.Create( hInst ) ) )
  105.         return 0;
  106.  
  107.     return d3dApp.Run();
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. //-----------------------------------------------------------------------------
  115. // Name: CMyD3DApplication()
  116. // Desc: Constructor
  117. //-----------------------------------------------------------------------------
  118. CMyD3DApplication::CMyD3DApplication()
  119. {
  120.     // Override base class members
  121.     m_strWindowTitle         = _T("DolphinVS: Tweening Vertex Shader");
  122.     m_bUseDepthBuffer        = TRUE;
  123.  
  124.     m_pFont                  = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  125.  
  126.     // Dolphin object
  127.     m_pDolphinTexture        = NULL;
  128.     m_pDolphinVB1            = NULL;
  129.     m_pDolphinVB2            = NULL;
  130.     m_pDolphinVB3            = NULL;
  131.     m_pDolphinIB             = NULL;
  132.     m_dwDolphinVertexShader  = 0L;
  133.     m_dwDolphinVertexShader2 = 0L;
  134.  
  135.     // SeaFloor object
  136.     m_pSeaFloorTexture       = NULL;
  137.     m_pSeaFloorVB            = NULL;
  138.     m_pSeaFloorIB            = NULL;
  139.     m_dwSeaFloorVertexShader = 0L;
  140.     m_dwSeaFloorVertexShader2= 0L;
  141.  
  142.     // Water caustics
  143.     for( DWORD t=0; t<32; t++ )
  144.         m_pCausticTextures[t] = NULL;
  145. }
  146.  
  147.  
  148.  
  149.  
  150. //-----------------------------------------------------------------------------
  151. // Name: OneTimeSceneInit()
  152. // Desc: Called during initial app startup, this function performs all the
  153. //       permanent initialization.
  154. //-----------------------------------------------------------------------------
  155. HRESULT CMyD3DApplication::OneTimeSceneInit()
  156. {
  157.     return S_OK;
  158. }
  159.  
  160.  
  161.  
  162.  
  163. //-----------------------------------------------------------------------------
  164. // Name: FrameMove()
  165. // Desc: Called once per frame, the call is the entry point for animating
  166. //       the scene.
  167. //-----------------------------------------------------------------------------
  168. HRESULT CMyD3DApplication::FrameMove()
  169. {
  170.     // Animation attributes for the dolphin
  171.     FLOAT fKickFreq    = 2*m_fTime;
  172.     FLOAT fPhase       = m_fTime/3;
  173.     FLOAT fBlendWeight = sinf( fKickFreq );
  174.  
  175.     // Move the dolphin in a circle
  176.     D3DXMATRIX matDolphin, matTrans, matRotate1, matRotate2;
  177.     D3DXMatrixScaling( &matDolphin, 0.01f, 0.01f, 0.01f );
  178.     D3DXMatrixRotationZ( &matRotate1, -cosf(fKickFreq)/6 );
  179.     D3DXMatrixMultiply( &matDolphin, &matDolphin, &matRotate1 );
  180.     D3DXMatrixRotationY( &matRotate2, fPhase );
  181.     D3DXMatrixMultiply( &matDolphin, &matDolphin, &matRotate2 );
  182.     D3DXMatrixTranslation( &matTrans, -5*sinf(fPhase), sinf(fKickFreq)/2, 10-10*cosf(fPhase) );
  183.     D3DXMatrixMultiply( &matDolphin, &matDolphin, &matTrans );
  184.  
  185.     // Animate the caustic textures
  186.     DWORD tex = ((DWORD)(m_fTime*32))%32;
  187.     m_pCurrentCausticTexture = m_pCausticTextures[tex];
  188.  
  189.     // Set the vertex shader constants. Note: outside of the blend matrices,
  190.     // most of these values don't change, so don't need to really be set every
  191.     // frame. It's just done here for clarity
  192.     {
  193.         // Some basic constants
  194.         D3DXVECTOR4 vZero( 0.0f, 0.0f, 0.0f, 0.0f );
  195.         D3DXVECTOR4 vOne( 1.0f, 0.5f, 0.2f, 0.05f );
  196.  
  197.         FLOAT fWeight1;
  198.         FLOAT fWeight2;
  199.         FLOAT fWeight3;
  200.  
  201.         if( fBlendWeight > 0.0f )
  202.         {
  203.             fWeight1 = fabsf(fBlendWeight);
  204.             fWeight2 = 1.0f - fabsf(fBlendWeight);
  205.             fWeight3 = 0.0f;
  206.         }
  207.         else
  208.         {
  209.             fWeight1 = 0.0f;
  210.             fWeight2 = 1.0f - fabsf(fBlendWeight);
  211.             fWeight3 = fabsf(fBlendWeight);
  212.         }
  213.         D3DXVECTOR4 vWeight( fWeight1, fWeight2, fWeight3, 0.0f );
  214.  
  215.         // Lighting vectors (in world space and in dolphin model space)
  216.         // and other constants
  217.         FLOAT fLight[]    = { 0.0f,  1.0f, 0.0f, 0.0f };
  218.         FLOAT fLightDolphinSpace[]    = { 0.0f,  1.0f, 0.0f, 0.0f };
  219.         FLOAT fDiffuse[]  = { 1.00f, 1.00f, 1.00f, 1.00f };
  220.         FLOAT fAmbient[]  = { 0.25f, 0.25f, 0.25f, 0.25f };
  221.         FLOAT fFog[]      = { 0.5f, 50.0f, 1.0f/(50.0f-1.0f), 0.0f };
  222.         FLOAT fCaustics[] = { 0.05f, 0.05f, sinf(m_fTime)/8, cosf(m_fTime)/10 };
  223.  
  224.         D3DXMATRIX matDolphinInv;
  225.         D3DXMatrixInverse(&matDolphinInv, NULL, &matDolphin);
  226.         D3DXVec4Transform((D3DXVECTOR4*)fLightDolphinSpace, (D3DXVECTOR4*)fLight, &matDolphinInv);
  227.         D3DXVec4Normalize((D3DXVECTOR4*)fLightDolphinSpace, (D3DXVECTOR4*)fLightDolphinSpace);
  228.  
  229.         // Vertex shader operations use transposed matrices
  230.         D3DXMATRIX mat, matCamera, matTranspose, matCameraTranspose;
  231.         D3DXMATRIX matViewTranspose, matProjTranspose;
  232.         D3DXMatrixMultiply(&matCamera, &matDolphin, &m_matView);
  233.         D3DXMatrixMultiply(&mat, &matCamera, &m_matProj);
  234.         D3DXMatrixTranspose(&matTranspose, &mat);
  235.         D3DXMatrixTranspose(&matCameraTranspose, &matCamera);
  236.         D3DXMatrixTranspose(&matViewTranspose, &m_matView);
  237.         D3DXMatrixTranspose(&matProjTranspose, &m_matProj);
  238.  
  239.         // Set the vertex shader constants
  240.         m_pd3dDevice->SetVertexShaderConstant(  0, &vZero,     1 );
  241.         m_pd3dDevice->SetVertexShaderConstant(  1, &vOne,      1 );
  242.         m_pd3dDevice->SetVertexShaderConstant(  2, &vWeight,   1 );
  243.         m_pd3dDevice->SetVertexShaderConstant(  4, &matTranspose, 4 );
  244.         m_pd3dDevice->SetVertexShaderConstant(  8, &matCameraTranspose,  4 );
  245.         m_pd3dDevice->SetVertexShaderConstant( 12, &matViewTranspose,  4 );
  246.         m_pd3dDevice->SetVertexShaderConstant( 19, &fLightDolphinSpace,   1 );
  247.         m_pd3dDevice->SetVertexShaderConstant( 20, &fLight,    1 );
  248.         m_pd3dDevice->SetVertexShaderConstant( 21, &fDiffuse,  1 );
  249.         m_pd3dDevice->SetVertexShaderConstant( 22, &fAmbient,  1 );
  250.         m_pd3dDevice->SetVertexShaderConstant( 23, &fFog,      1 );
  251.         m_pd3dDevice->SetVertexShaderConstant( 24, &fCaustics, 1 );
  252.         m_pd3dDevice->SetVertexShaderConstant( 28, &matProjTranspose,  4 );
  253.     }
  254.  
  255.     return S_OK;
  256. }
  257.  
  258.  
  259.  
  260.  
  261. //-----------------------------------------------------------------------------
  262. // Name: Render()
  263. // Desc: Called once per frame, the call is the entry point for 3d
  264. //       rendering. This function sets up render states, clears the
  265. //       viewport, and renders the scene.
  266. //-----------------------------------------------------------------------------
  267. HRESULT CMyD3DApplication::Render()
  268. {
  269.     // Clear the viewport
  270.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  271.                          WATER_COLOR, 1.0f, 0L );
  272.  
  273.     // Begin the scene
  274.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  275.     {
  276.         FLOAT fAmbientLight[]  = { 0.25f, 0.25f, 0.25f, 0.25f };
  277.         m_pd3dDevice->SetVertexShaderConstant( 22, &fAmbientLight, 1 );
  278.  
  279.         // Render the seafloor
  280.         m_pd3dDevice->SetTexture( 0, m_pSeaFloorTexture );
  281.         m_pd3dDevice->SetVertexShader( m_dwSeaFloorVertexShader );
  282.         m_pd3dDevice->SetStreamSource( 0, m_pSeaFloorVB, sizeof(D3DVERTEX) );
  283.         m_pd3dDevice->SetIndices( m_pSeaFloorIB, 0 );
  284.         m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  285.                                             0, m_dwNumSeaFloorVertices,
  286.                                             0, m_dwNumSeaFloorFaces );
  287.  
  288.         // Render the dolphin
  289.         m_pd3dDevice->SetTexture( 0, m_pDolphinTexture );
  290.         m_pd3dDevice->SetVertexShader( m_dwDolphinVertexShader );
  291.         m_pd3dDevice->SetStreamSource( 0, m_pDolphinVB1, sizeof(D3DVERTEX) );
  292.         m_pd3dDevice->SetStreamSource( 1, m_pDolphinVB2, sizeof(D3DVERTEX) );
  293.         m_pd3dDevice->SetStreamSource( 2, m_pDolphinVB3, sizeof(D3DVERTEX) );
  294.         m_pd3dDevice->SetIndices( m_pDolphinIB, 0 );
  295.         m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  296.                                             0, m_dwNumDolphinVertices,
  297.                                             0, m_dwNumDolphinFaces );
  298.  
  299.         // Now, we are going to do a 2nd pass, to alpha-blend in the caustics.
  300.         // The caustics use a 2nd set of texture coords that are generated
  301.         // by the vertex shaders. Lighting from the light above is used, but
  302.         // ambient is turned off to avoid lighting objects from below (for
  303.         // instance, we don't want caustics appearing on the dolphin's
  304.         // underbelly). Finally, fog color is set to black, so that caustics
  305.         // fade in distance.
  306.  
  307.         // Turn on alpha blending
  308.         m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
  309.         
  310.         m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_ONE );
  311.         m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
  312.  
  313.         // Setup the caustic texture
  314.         m_pd3dDevice->SetTexture( 0, m_pCurrentCausticTexture );
  315.  
  316.         // Set ambient and fog colors to black
  317.         FLOAT fAmbientDark[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  318.         m_pd3dDevice->SetVertexShaderConstant( 22, &fAmbientDark, 1 );
  319.         m_pd3dDevice->SetRenderState( D3DRS_FOGCOLOR, 0x00000000 );
  320.  
  321.         // Render the caustic effects for the seafloor
  322.         m_pd3dDevice->SetVertexShader( m_dwSeaFloorVertexShader2 );
  323.         m_pd3dDevice->SetStreamSource( 0, m_pSeaFloorVB, sizeof(D3DVERTEX) );
  324.         m_pd3dDevice->SetIndices( m_pSeaFloorIB, 0 );
  325.         m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  326.                                             0, m_dwNumSeaFloorVertices,
  327.                                             0, m_dwNumSeaFloorFaces );
  328.  
  329.         // Finally, render the caustic effects for the dolphin
  330.         m_pd3dDevice->SetVertexShader( m_dwDolphinVertexShader2 );
  331.         m_pd3dDevice->SetStreamSource( 0, m_pDolphinVB1, sizeof(D3DVERTEX) );
  332.         m_pd3dDevice->SetStreamSource( 1, m_pDolphinVB2, sizeof(D3DVERTEX) );
  333.         m_pd3dDevice->SetStreamSource( 2, m_pDolphinVB3, sizeof(D3DVERTEX) );
  334.         m_pd3dDevice->SetIndices( m_pDolphinIB, 0 );
  335.         m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  336.                                             0, m_dwNumDolphinVertices,
  337.                                             0, m_dwNumDolphinFaces );
  338.  
  339.         // Restore modified render states
  340.         m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
  341.         m_pd3dDevice->SetRenderState( D3DRS_FOGCOLOR, WATER_COLOR );
  342.  
  343.         // Output statistics
  344.         m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  345.         m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  346.  
  347.         // End the scene.
  348.         m_pd3dDevice->EndScene();
  349.     }
  350.  
  351.     return S_OK;
  352. }
  353.  
  354.  
  355.  
  356.  
  357. //-----------------------------------------------------------------------------
  358. // Name: InitDeviceObjects()
  359. // Desc: Initialize device-dependent objects. This is the place to create mesh
  360. //       and texture objects.
  361. //-----------------------------------------------------------------------------
  362. HRESULT CMyD3DApplication::InitDeviceObjects()
  363. {
  364.     LPDIRECT3DVERTEXBUFFER8 pMeshSourceVB;
  365.     LPDIRECT3DINDEXBUFFER8  pMeshSourceIB;
  366.     D3DVERTEX*              pSrc;
  367.     D3DVERTEX*              pDst;
  368.     CD3DMesh                DolphinMesh01;
  369.     CD3DMesh                DolphinMesh02;
  370.     CD3DMesh                DolphinMesh03;
  371.     CD3DMesh                SeaFloorMesh;
  372.  
  373.     // Initialize the font's internal textures
  374.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  375.  
  376.     // Create texture for the dolphin
  377.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Dolphin.bmp"),
  378.                                        &m_pDolphinTexture ) ) )
  379.     {
  380.         return D3DAPPERR_MEDIANOTFOUND;
  381.     }
  382.  
  383.     // Create textures for the seafloor
  384.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("SeaFloor.bmp"),
  385.                                        &m_pSeaFloorTexture ) ) )
  386.     {
  387.         return D3DAPPERR_MEDIANOTFOUND;
  388.     }
  389.  
  390.     // Create textures for the water caustics
  391.     for( DWORD t=0; t<32; t++ )
  392.     {
  393.         TCHAR strName[80];
  394.         sprintf( strName, _T("Caust%02ld.tga"), t );
  395.         if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, strName,
  396.                                            &m_pCausticTextures[t] ) ) )
  397.         {
  398.             return D3DAPPERR_MEDIANOTFOUND;
  399.         }
  400.     }
  401.  
  402.     // Load the file-based mesh objects
  403.     if( FAILED( DolphinMesh01.Create( m_pd3dDevice, _T("dolphin1.x") ) ) )
  404.         return D3DAPPERR_MEDIANOTFOUND;
  405.     if( FAILED( DolphinMesh02.Create( m_pd3dDevice, _T("dolphin2.x") ) ) )
  406.         return D3DAPPERR_MEDIANOTFOUND;
  407.     if( FAILED( DolphinMesh03.Create( m_pd3dDevice, _T("dolphin3.x") ) ) )
  408.         return D3DAPPERR_MEDIANOTFOUND;
  409.     if( FAILED( SeaFloorMesh.Create( m_pd3dDevice, _T("SeaFloor.x") ) ) )
  410.         return D3DAPPERR_MEDIANOTFOUND;
  411.  
  412.     // Set the FVF type to match the vertex format we want
  413.     DolphinMesh01.SetFVF( m_pd3dDevice, D3DFVF_D3DVERTEX );
  414.     DolphinMesh02.SetFVF( m_pd3dDevice, D3DFVF_D3DVERTEX );
  415.     DolphinMesh03.SetFVF( m_pd3dDevice, D3DFVF_D3DVERTEX );
  416.     SeaFloorMesh.SetFVF(  m_pd3dDevice, D3DFVF_D3DVERTEX );
  417.  
  418.     // Get the number of vertices and faces for the meshes
  419.     m_dwNumDolphinVertices  = DolphinMesh01.GetSysMemMesh()->GetNumVertices();
  420.     m_dwNumDolphinFaces     = DolphinMesh01.GetSysMemMesh()->GetNumFaces();
  421.     m_dwNumSeaFloorVertices = SeaFloorMesh.GetSysMemMesh()->GetNumVertices();
  422.     m_dwNumSeaFloorFaces    = SeaFloorMesh.GetSysMemMesh()->GetNumFaces();
  423.  
  424.     // Create the dolphin and seafloor vertex and index buffers
  425.     m_pd3dDevice->CreateVertexBuffer( m_dwNumDolphinVertices * sizeof(D3DVERTEX),
  426.                                       D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
  427.                                       &m_pDolphinVB1 );
  428.     m_pd3dDevice->CreateVertexBuffer( m_dwNumDolphinVertices * sizeof(D3DVERTEX),
  429.                                       D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
  430.                                       &m_pDolphinVB2 );
  431.     m_pd3dDevice->CreateVertexBuffer( m_dwNumDolphinVertices * sizeof(D3DVERTEX),
  432.                                       D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
  433.                                       &m_pDolphinVB3 );
  434.     m_pd3dDevice->CreateVertexBuffer( m_dwNumSeaFloorVertices * sizeof(D3DVERTEX),
  435.                                       D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
  436.                                       &m_pSeaFloorVB );
  437.     m_pd3dDevice->CreateIndexBuffer( m_dwNumDolphinFaces * 3 * sizeof(WORD),
  438.                                       D3DUSAGE_WRITEONLY,
  439.                                       D3DFMT_INDEX16, D3DPOOL_MANAGED,
  440.                                       &m_pDolphinIB );
  441.     m_pd3dDevice->CreateIndexBuffer( m_dwNumSeaFloorFaces * 3 * sizeof(WORD),
  442.                                       D3DUSAGE_WRITEONLY,
  443.                                       D3DFMT_INDEX16, D3DPOOL_MANAGED,
  444.                                       &m_pSeaFloorIB );
  445.  
  446.     // Copy vertices for mesh 01
  447.     DolphinMesh01.GetSysMemMesh()->GetVertexBuffer( &pMeshSourceVB );
  448.     m_pDolphinVB1->Lock( 0, 0, (BYTE**)&pDst, 0 );
  449.     pMeshSourceVB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  450.     memcpy( pDst, pSrc, m_dwNumDolphinVertices * sizeof(D3DVERTEX) );
  451.     m_pDolphinVB1->Unlock();
  452.     pMeshSourceVB->Unlock();
  453.     pMeshSourceVB->Release();
  454.  
  455.     // Copy vertices for mesh 2
  456.     DolphinMesh02.GetSysMemMesh()->GetVertexBuffer( &pMeshSourceVB );
  457.     m_pDolphinVB2->Lock( 0, 0, (BYTE**)&pDst, 0 );
  458.     pMeshSourceVB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  459.     memcpy( pDst, pSrc, m_dwNumDolphinVertices * sizeof(D3DVERTEX) );
  460.     m_pDolphinVB2->Unlock();
  461.     pMeshSourceVB->Unlock();
  462.     pMeshSourceVB->Release();
  463.  
  464.     // Copy vertices for mesh 3
  465.     DolphinMesh03.GetSysMemMesh()->GetVertexBuffer( &pMeshSourceVB );
  466.     m_pDolphinVB3->Lock( 0, 0, (BYTE**)&pDst, 0 );
  467.     pMeshSourceVB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  468.     memcpy( pDst, pSrc, m_dwNumDolphinVertices * sizeof(D3DVERTEX) );
  469.     m_pDolphinVB3->Unlock();
  470.     pMeshSourceVB->Unlock();
  471.     pMeshSourceVB->Release();
  472.  
  473.     // Copy vertices for the seafloor mesh, and add some bumpiness
  474.     SeaFloorMesh.GetSysMemMesh()->GetVertexBuffer( &pMeshSourceVB );
  475.     m_pSeaFloorVB->Lock( 0, 0, (BYTE**)&pDst, 0 );
  476.     pMeshSourceVB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  477.     memcpy( pDst, pSrc, m_dwNumSeaFloorVertices * sizeof(D3DVERTEX) );
  478.     srand(5);
  479.     for( DWORD i=0; i<m_dwNumSeaFloorVertices; i++ )
  480.     {
  481.         ((D3DVERTEX*)pDst)[i].p.y += (rand()/(FLOAT)RAND_MAX);
  482.         ((D3DVERTEX*)pDst)[i].p.y += (rand()/(FLOAT)RAND_MAX);
  483.         ((D3DVERTEX*)pDst)[i].p.y += (rand()/(FLOAT)RAND_MAX);
  484.         ((D3DVERTEX*)pDst)[i].tu  *= 10;
  485.         ((D3DVERTEX*)pDst)[i].tv  *= 10;
  486.     }
  487.     m_pSeaFloorVB->Unlock();
  488.     pMeshSourceVB->Unlock();
  489.     pMeshSourceVB->Release();
  490.  
  491.     // Copy indices for the dolphin mesh
  492.     DolphinMesh01.GetSysMemMesh()->GetIndexBuffer( &pMeshSourceIB );
  493.     m_pDolphinIB->Lock( 0, 0, (BYTE**)&pDst, 0 );
  494.     pMeshSourceIB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  495.     memcpy( pDst, pSrc, 3 * m_dwNumDolphinFaces * sizeof(WORD) );
  496.     m_pDolphinIB->Unlock();
  497.     pMeshSourceIB->Unlock();
  498.     pMeshSourceIB->Release();
  499.  
  500.     // Copy indices for the seafloor mesh
  501.     SeaFloorMesh.GetSysMemMesh()->GetIndexBuffer( &pMeshSourceIB );
  502.     m_pSeaFloorIB->Lock( 0, 0, (BYTE**)&pDst, 0 );
  503.     pMeshSourceIB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  504.     memcpy( pDst, pSrc, 3 * m_dwNumSeaFloorFaces * sizeof(WORD) );
  505.     m_pSeaFloorIB->Unlock();
  506.     pMeshSourceIB->Unlock();
  507.     pMeshSourceIB->Release();
  508.  
  509.     return S_OK;
  510. }
  511.  
  512.  
  513.  
  514.  
  515. //-----------------------------------------------------------------------------
  516. // Name: RestoreDeviceObjects()
  517. // Desc: Restore device-memory objects and state after a device is created or
  518. //       resized.
  519. //-----------------------------------------------------------------------------
  520. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  521. {
  522.     HRESULT hr;
  523.  
  524.     m_pFont->RestoreDeviceObjects();
  525.  
  526.     // Set the transform matrices
  527.     D3DXVECTOR3 vEyePt      = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
  528.     D3DXVECTOR3 vLookatPt   = D3DXVECTOR3( 0.0f, 0.0f,  0.0f );
  529.     D3DXVECTOR3 vUpVec      = D3DXVECTOR3( 0.0f, 1.0f,  0.0f );
  530.     FLOAT       fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
  531.     D3DXMatrixIdentity( &m_matWorld );
  532.     D3DXMatrixLookAtLH( &m_matView, &vEyePt, &vLookatPt, &vUpVec );
  533.     D3DXMatrixPerspectiveFovLH( &m_matProj, D3DX_PI/3, fAspect, 1.0f, 10000.0f );
  534.  
  535.     // Set default render states
  536.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  537.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  538.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
  539.     m_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,      TRUE );
  540.     m_pd3dDevice->SetRenderState( D3DRS_FOGCOLOR,       WATER_COLOR );
  541.  
  542.     // Create vertex shader for the dolphin
  543.     DWORD dwDolphinVertexDecl[] =
  544.     {
  545.         D3DVSD_STREAM( 0 ),
  546.         D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position of first mesh
  547.         D3DVSD_REG( 3, D3DVSDT_FLOAT3 ), // Normal
  548.         D3DVSD_REG( 6, D3DVSDT_FLOAT2 ), // Tex coords
  549.         D3DVSD_STREAM( 1 ),
  550.         D3DVSD_REG( 1, D3DVSDT_FLOAT3 ), // Position of second mesh
  551.         D3DVSD_REG( 4, D3DVSDT_FLOAT3 ), // Normal
  552.         D3DVSD_REG( 7, D3DVSDT_FLOAT2 ), // Tex coords
  553.         D3DVSD_STREAM( 2 ),
  554.         D3DVSD_REG( 2, D3DVSDT_FLOAT3 ), // Position of second mesh
  555.         D3DVSD_REG( 5, D3DVSDT_FLOAT3 ), // Normal
  556.         D3DVSD_REG( 8, D3DVSDT_FLOAT2 ), // Tex coords
  557.         D3DVSD_END()
  558.     };
  559.  
  560.     if( FAILED( hr = D3DUtil_CreateVertexShader( m_pd3dDevice, _T("DolphinTween.vsh"),
  561.                                                  dwDolphinVertexDecl,
  562.                                                  &m_dwDolphinVertexShader ) ) )
  563.     {
  564.         return hr;
  565.     }
  566.  
  567.     if( FAILED( hr = D3DUtil_CreateVertexShader( m_pd3dDevice, _T("DolphinTween2.vsh"),
  568.                                                  dwDolphinVertexDecl,
  569.                                                  &m_dwDolphinVertexShader2 ) ) )
  570.     {
  571.         return hr;
  572.     }
  573.  
  574.     // Create vertex shader for the seafloor
  575.     DWORD dwSeaFloorVertexDecl[] =
  576.     {
  577.         D3DVSD_STREAM( 0 ),
  578.         D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position of first mesh
  579.         D3DVSD_REG( 3, D3DVSDT_FLOAT3 ), // Normal
  580.         D3DVSD_REG( 6, D3DVSDT_FLOAT2 ), // Tex coords
  581.         D3DVSD_END()
  582.     };
  583.  
  584.     if( FAILED( hr = D3DUtil_CreateVertexShader( m_pd3dDevice, _T("SeaFloor.vsh"),
  585.                                                  dwSeaFloorVertexDecl,
  586.                                                  &m_dwSeaFloorVertexShader ) ) )
  587.     {
  588.         return hr;
  589.     }
  590.  
  591.     if( FAILED( hr = D3DUtil_CreateVertexShader( m_pd3dDevice, _T("SeaFloor2.vsh"),
  592.                                                  dwSeaFloorVertexDecl,
  593.                                                  &m_dwSeaFloorVertexShader2 ) ) )
  594.     {
  595.         return hr;
  596.     }
  597.  
  598.     return S_OK;
  599. }
  600.  
  601.  
  602.  
  603.  
  604. //-----------------------------------------------------------------------------
  605. // Name: InvalidateDeviceObjects()
  606. // Desc: Called when the device-dependent objects are about to be lost.
  607. //-----------------------------------------------------------------------------
  608. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  609. {
  610.     m_pFont->InvalidateDeviceObjects();
  611.  
  612.     // Clean up vertex shaders
  613.     if( m_dwDolphinVertexShader != 0 )
  614.     {
  615.         m_pd3dDevice->DeleteVertexShader( m_dwDolphinVertexShader );
  616.         m_dwDolphinVertexShader = 0;
  617.     }
  618.  
  619.     if( m_dwDolphinVertexShader2 != 0 )
  620.     {
  621.         m_pd3dDevice->DeleteVertexShader( m_dwDolphinVertexShader2 );
  622.         m_dwDolphinVertexShader2 = 0;
  623.     }
  624.     
  625.     if( m_dwSeaFloorVertexShader != 0 )
  626.     {
  627.         m_pd3dDevice->DeleteVertexShader( m_dwSeaFloorVertexShader );
  628.         m_dwSeaFloorVertexShader = 0;
  629.     }
  630.     
  631.     if( m_dwSeaFloorVertexShader2 != 0 )
  632.     {
  633.         m_pd3dDevice->DeleteVertexShader( m_dwSeaFloorVertexShader2 );
  634.         m_dwSeaFloorVertexShader2 = 0;
  635.     }
  636.  
  637.     return S_OK;
  638. }
  639.  
  640.  
  641.  
  642.  
  643. //-----------------------------------------------------------------------------
  644. // Name: DeleteDeviceObjects()
  645. // Desc: Called when the app is exiting, or the device is being changed,
  646. //       this function deletes any device dependent objects.
  647. //-----------------------------------------------------------------------------
  648. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  649. {
  650.     m_pFont->DeleteDeviceObjects();
  651.  
  652.     // Clean up dolphin objects
  653.     SAFE_RELEASE( m_pDolphinTexture );
  654.     SAFE_RELEASE( m_pDolphinVB1 );
  655.     SAFE_RELEASE( m_pDolphinVB2 );
  656.     SAFE_RELEASE( m_pDolphinVB3 );
  657.     SAFE_RELEASE( m_pDolphinIB );
  658.  
  659.     // Clean up seafoor objects
  660.     SAFE_RELEASE( m_pSeaFloorTexture );
  661.     SAFE_RELEASE( m_pSeaFloorVB );
  662.     SAFE_RELEASE( m_pSeaFloorIB );
  663.  
  664.     // Clean up textures for water caustics
  665.     for( DWORD i=0; i<32; i++ )
  666.         SAFE_RELEASE( m_pCausticTextures[i] );
  667.  
  668.     return S_OK;
  669. }
  670.  
  671.  
  672.  
  673.  
  674. //-----------------------------------------------------------------------------
  675. // Name: FinalCleanup()
  676. // Desc: Called before the app exits, this function gives the app the chance
  677. //       to cleanup after itself.
  678. //-----------------------------------------------------------------------------
  679. HRESULT CMyD3DApplication::FinalCleanup()
  680. {
  681.     SAFE_DELETE( m_pFont );
  682.     return S_OK;
  683. }
  684.  
  685.  
  686.  
  687.  
  688. //-----------------------------------------------------------------------------
  689. // Name: ConfirmDevice()
  690. // Desc: Called during device initialization, this code checks the device
  691. //       for some minimum set of capabilities
  692. //-----------------------------------------------------------------------------
  693. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  694.                                           D3DFORMAT Format )
  695. {
  696.     if( ( dwBehavior & D3DCREATE_HARDWARE_VERTEXPROCESSING ) ||
  697.         ( dwBehavior & D3DCREATE_MIXED_VERTEXPROCESSING ) )
  698.     {
  699.         if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,0) )
  700.             return E_FAIL;
  701.     }
  702.  
  703.     return S_OK;
  704. }
  705.  
  706.  
  707.  
  708.  
  709.